package com.example.ymm.modules1.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.model.em.EmSysNotePlatform;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.query.DbUserWaitQueryCriteria;
import com.example.ymm.modules1.service.dto.*;
import com.example.ymm.modules1.service.user.*;
import com.example.ymm.util.UtilValidate;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hl/sys/sysData")
@Tag(name= "系统信息管理")
@Slf4j
@CrossOrigin
public class SysDataController extends BaseController {

    @Autowired
    DbDeptService dbDeptService;

    @Autowired
    DbCardService dbCardService;

    @Autowired
    DbDeptUserService dbDeptUserService;
    @Autowired
    DbChatGroupService dbChatGroupService;

    @Autowired
    DbIntegralLogService dbIntegralLogService;

    @Autowired
    DbUserNoteService dbUserNoteService;

    @Autowired
    DbUserWaitService dbUserWaitService;




    @RequestMapping(value = "/getAllDeptList", method = {RequestMethod.POST})
    @Operation(summary= "获取所有部门", description = "树形")
    public CommonResult getAllDeptList(
            DbDeptQueryCriteria criteria,
            HttpServletRequest request, HttpServletResponse response){
        try {
            List<DbDept> deptTreeList = dbDeptService.getDeptTreeList(criteria);
            return CommonResult.success(deptTreeList);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/getAllDept", method = {RequestMethod.POST})
    @Operation(summary= "获取所有部门", description = "非树形")
    public CommonResult getAllDept(
            DbDeptQueryCriteria criteria,
            HttpServletRequest request, HttpServletResponse response){
        try {
            List<DbDept> dbDepts = dbDeptService.queryAll(criteria);
            return CommonResult.success(dbDepts);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/deptUserList", method = {RequestMethod.POST})
    @Operation(summary= "获取人员-部门信息", description = "分页")
    public CommonResult getAllDeptList(
            DbDeptUserQueryCriteria criteria, YmPageDto pageDto,
            HttpServletRequest request, HttpServletResponse response){
        try {
            TableList<ViewDeptUser> deptUserList = dbDeptUserService.getDeptUserList(criteria, pageDto);
            return CommonResult.success(deptUserList);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }


    @Operation(summary= "导入 钉钉部门花名册")
    @RequestMapping(value = "/importDepts", method = RequestMethod.POST)
    public CommonResult importDepts(@RequestBody ImportDeptExcelDto dto) {
        try{
            if(UtilValidate.isEmpty(dto.getList())){
                throw new YmException("数据不能为空！");
            }
            List<Map<String, Object>> maps = dbDeptService.importDepts(dto);
            return CommonResult.success(maps);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @RequestMapping(value = "/queryGroupChatUsers", method = {RequestMethod.POST})
    @Operation(summary= "获取查询群成员信息", description = "所有")
    public CommonResult queryGroupChatUsers(
            String chatid,
            HttpServletRequest request, HttpServletResponse response){
        try {
            List<DbUser> dbUsers = dbChatGroupService.queryGroupChatUsers(chatid);
            return CommonResult.success(dbUsers);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }


    @RequestMapping(value = "/syncDeptList", method = {RequestMethod.POST})
    @Operation(summary= "同步部门信息", description = "测试标题")
    public CommonResult syncDeptList(
            HttpServletRequest request, HttpServletResponse response){
        try {
            dbDeptService.syncUpLoad();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @RequestMapping(value = "/integralList", method = {RequestMethod.POST})
    @Operation(summary= "积分明细列表", description = "分页")
    public CommonResult integralList(
            DbIntegralLogQueryCriteria criteria, YmPageDto pageDto,
            HttpServletRequest request, HttpServletResponse response){
        try {
            TableList<DbIntegralLog> list = dbIntegralLogService.list(criteria, pageDto);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/cardList", method = {RequestMethod.POST})
    @Operation(summary= "打卡记录列表", description = "分页")
    public CommonResult cardList(
            DbCardQueryCriteria criteria, YmPageDto pageDto,
            HttpServletRequest request, HttpServletResponse response){
        try {
            TableList<DbCard> list1 = dbCardService.list(criteria, pageDto);
            return CommonResult.success(list1);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }



    @RequestMapping(value = "/sysRankingList", method = {RequestMethod.POST})
    @Operation(summary= "积分明细列表", description = "分页")
    public CommonResult sysRankingList(
            SysRankingQueryCriteria criteria, YmPageDto pageDto,
            HttpServletRequest request, HttpServletResponse response){
        try {
            TableList<SysRankingListDto> list = null;
            // 1 打卡 2总积分 3读书积分 4时长排行
            if(criteria.getRankingType()==1){
                list = dbCardService.sysRankingList(criteria, pageDto);
            }else if(criteria.getRankingType()==2){
                list = dbIntegralLogService.rankingIntegralList(criteria, pageDto);
            }else if(criteria.getRankingType()==3){
                list = dbIntegralLogService.sysRankingList(criteria, pageDto);
            }else if(criteria.getRankingType()==4){
                list = dbUserNoteService.sysRankingList(criteria, pageDto);
            }
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/dutyTableList", method = {RequestMethod.POST})
    @Operation(summary= "出勤率统计", description = "分页")
    public CommonResult dutyTableList(
            RankingUserQueryCriteria criteria, YmPageDto pageDto,
            HttpServletRequest request, HttpServletResponse response){
        try {
            RankingTableUserDto rankingTableUserDto = dbCardService.dutyTableList(criteria, pageDto);
            return CommonResult.success(rankingTableUserDto);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }


    @RequestMapping(value = "/bookPieCount", method = {RequestMethod.POST})
    @Operation(summary= "书本阅读占比统计", description = "饼图")
    public CommonResult bookPieCount(
            SysRankingQueryCriteria criteria,
            HttpServletRequest request, HttpServletResponse response){
        try {
            List<BookPieDto> bookPieDtos = dbUserNoteService.bookPieCount(criteria);
            return CommonResult.success(bookPieDtos);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/bookUserTimeCount", method = {RequestMethod.POST})
    @Operation(summary= "首页统计", description = "卡片")
    public CommonResult bookUserTimeCount(
            SysRankingQueryCriteria criteria,
            HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String, Object> map = dbUserNoteService.bookUserTimeCount(criteria);
            return CommonResult.success(map);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/addDailyCardByDate", method = {RequestMethod.POST})
    @Operation(summary= "补正常卡")
    public CommonResult addDailyCardByDate(
            String userId,String date){
        try {
            DbCard dbCard = dbCardService.addUserCardByDate(userId, date, 2, EmSysNotePlatform.da_day.getType());
            return CommonResult.success(dbCard);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @RequestMapping(value = "/addYesCardByDate", method = {RequestMethod.POST})
    @Operation(summary= "补昨日卡")
    public CommonResult addYesCardByDate(
            String userId,String date,Integer integral,String sysNote){
        try {
            DbCard dbCard = dbCardService.addUserCardByDate(userId, date,integral,sysNote);
            return CommonResult.success(dbCard);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @PostMapping("/myWaitList")
    @Operation(summary= "我的代办")
    public CommonResult myWaitList(DbUserWaitQueryCriteria criteria, YmPageDto ymPage){
        try {
            String userId = getUserId();
            criteria.setUserId(userId);

            List<JSONObject> list = dbUserWaitService.myWaitList(criteria);

            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }


}
